/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.randis.dailyplanner.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.randis.dailyplanner.ModelAndView;
import com.randis.dailyplanner.ServiceLocator;
import com.randis.dailyplanner.domain.Activity;
import com.randis.dailyplanner.managers.ActivitiesManager;

/**
 *
 * @author Alexei Petinov
 */
public class ActivitiesController {

	private ActivitiesManager manager = ServiceLocator.getInstance().getActivitiesManager();

    public ActivitiesController() {
    }

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
    	UserService userService = UserServiceFactory.getUserService();
    	User user = userService.getCurrentUser();
    	
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("activities", manager.list());
        model.put("nickname", user.getNickname());
        return new ModelAndView(model, "/activities.jsp");
    }

    public ModelAndView editActivity(HttpServletRequest request, HttpServletResponse response) {
    	
    	String id = request.getParameter("id");
    	
    	Activity activity = null; 
    	if (StringUtils.isBlank(id)) {
    		activity = new Activity();
			activity.setDueDate(new Date());
    	}
    	else {
    		activity = manager.getActivity(id);
    	}
    	
    	Map<String, Object> model = new HashMap<String, Object>();
        model.put("activity", activity);
        model.put("dueDateString", new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(activity.getDueDate()));
        return new ModelAndView(model, "/edit_activity.jsp");
    }

    public ModelAndView saveActivity(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	String id = request.getParameter("id");
    	
    	Activity activity = null; 
    	if (StringUtils.isBlank(id) || 
    		id.equals("0")) {
    		activity = new Activity(); 
    	}
    	else {
    		activity = manager.getActivity(id);
    	}

    	BeanWrapper wrapper = new BeanWrapperImpl(activity);
    	CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"), false);
    	wrapper.registerCustomEditor(Date.class, dateEditor);
    	Map<String, Object> parameterMap = new HashMap<String, Object>(request.getParameterMap());
    	parameterMap.remove("id");
    	wrapper.setPropertyValues(parameterMap);
    	manager.saveActivity((Activity) wrapper.getWrappedInstance());
    	
    	response.sendRedirect("/controllers/activities/list");
    	
        return null;
    }

    public ModelAndView deleteActivity(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	String id = request.getParameter("id");    	
    	manager.deleteActivity(id);    	
    	response.sendRedirect("/controllers/activities/list");
    	
        return null;
    }
}